package com.customization.huangwm.workflowNodeAction.lims;

import com.alibaba.fastjson.JSONObject;
import com.customization.huangwm.utils.DBTools;
import com.customization.huangwm.utils.DateTools;
import com.customization.huangwm.utils.HttpRequest;
import com.customization.huangwm.utils.LogTool;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Huangwm
 * @Date 2022-06-23
 * @Version 1.0
 * @Description TODO SW004驗水試壓流程推送lims
 **/
public class NewPipelineRequest extends BaseBean implements Action {
    private LogTool log = new LogTool("/log/Lims/huangwm/action/NewPipelineRequest", false);
    private HttpRequest http = new HttpRequest();
    private DBTools dbTools = new DBTools();
    private DateTools dateTools = new DateTools();
    private String[] itemsid = {"00_21_151","00_21_154","00_21_126","00_21_157"};
    @Override
    public String execute(RequestInfo requestInfo) {
        log.writeLog("LIMS 驗水試壓流程推送lims requestid=" + requestInfo.getRequestid());
        String tablename = requestInfo.getRequestManager().getBillTableName();
        int mainid = requestInfo.getRequestManager().getBillid();
        try {
            RecordSet rs = new RecordSet();
            rs.execute("select t1.requestid,t2.creater,t2.createdate,t2.createtime from " + tablename + " t1 left join workflow_requestbase t2 on t1.requestid=t2.requestid\n" +
                    "where t1.requestid=" + requestInfo.getRequestid());
            rs.next();
            log.writeLog(JSONObject.toJSONString(rs.getArray()));
            String requestNo = rs.getString("requestid");
            String userCode = dbTools.getWorkCode(rs.getString("creater"));
            String requestPerson = dbTools.getLastname(rs.getString("creater"));

            rs.execute("select ypbh from "+tablename+"_dt4 where mainid="+mainid);
            rs.first();
            String sampleNo = rs.getString("ypbh");

            JSONObject reqData = new JSONObject();
            reqData.put("userCode", userCode);
            reqData.put("plantCode", "01");
            reqData.put("action", "newPipelineRequest");
            reqData.put("requestNo", requestNo);
            reqData.put("sampleNo", sampleNo);
            reqData.put("sampleType", "其他水樣");
            reqData.put("sampleName", "管道消毒樣品");
            reqData.put("collectPoint", "管道消毒樣品");
//            reqData.put("collectDate", rs.getString("createdate"));
//            reqData.put("collectTime", rs.getString("createtime").substring(0,5));
            reqData.put("collectDate", dateTools.getCurrentDate());
            reqData.put("collectTime", dateTools.getCurrentTime().substring(0,5));
            reqData.put("requestPerson", requestPerson);
            reqData.put("requestTime", requestInfo.getRequestManager().getCurrentDate() + " " + requestInfo.getRequestManager().getCurrentTime());
            reqData.put("comments", "");
            List<Map<String, String>> items = new ArrayList<>();
            for (int i = 0; i < itemsid.length; i++) {
                Map<String, String> item = new HashMap<>();
                item.put("itemId", itemsid[i]);
                item.put("result", itemsid[i].equals("00_21_151") ? "999" : "");
                items.add(item);
            }
            reqData.put("items", items);

            JSONObject allParams = new JSONObject();//所有的请求参数
            allParams.put("reqNb", dateTools.getTimestamp());
            allParams.put("reqLang", "zh");
            String token = getPropValue("LIMS", "eop_reqToken").isEmpty() ? "EapLims20200708D1207F4TEST" : getPropValue("LIMS", "eop_reqToken");
            String url = getPropValue("LIMS", "eop_reqUrl").isEmpty() ? "https://testeop.macaowater.com" : getPropValue("LIMS", "eop_reqUrl");
            allParams.put("reqToken", token);
            allParams.put("reqData", reqData);
            log.writeLog("请求参数>>>" + JSONObject.toJSONString(allParams));
            JSONObject requestResult = http.CallApiPost(url + "/eop/eapToLims/eapapi", JSONObject.toJSONString(allParams));
            log.writeLog("请求结果>>>" + JSONObject.toJSONString(requestResult));

            if (!requestResult.get("state").equals("0") || requestResult == null) {
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent(requestResult.get("msg").toString());
                return FAILURE_AND_CONTINUE;
            }
            return SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return FAILURE_AND_CONTINUE;
        }
    }
}
